package org.subject.controller;

import com.sun.scenario.effect.impl.prism.PrImage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.subject.dto.MessageAndUser;
import org.subject.entity.Article;
import org.subject.entity.Message;
import org.subject.entity.Page;
import org.subject.entity.User;
import org.subject.service.ArticleService;
import org.subject.service.impl.MessageServiceImpl;
import org.subject.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 登录控制器
 * Created by kai on 2017/5/3.
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private MessageServiceImpl messageService;
    @Autowired
    private ArticleService articleService;
    private List<Message> messageList;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String LoginPage(Model model) {
        return "login";
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    public String VerUser(@ModelAttribute("userName") String userName, @ModelAttribute("password") String password, HttpServletRequest req, Model model) {
//        logger.info("参数：" + userName + password);
        //查询是否是已注册用户
        List<User> userList = userService.queryAllUser();
        if (userList != null) {
            for (User user : userList) {
                if (!userName.trim().equals("") && userName.trim().equals(user.getUserNumber())
                        && !userName.trim().equals("") && userName.trim().equals(user.getUserNumber())) {
                    req.getSession().setAttribute("user", user);
                    int pages = 1;
                    List<MessageAndUser> messageAndUsers = messageService.queryMessageList("", "", pages);
                    model.addAttribute("list", messageAndUsers);
                    int count = messageService.queryMessageCount("", "");
                    Page page = new Page();
                    page.setTotalNumber(count);
                    model.addAttribute("page", page);
                    List<Article> articleList = articleService.queryArticleList("", pages);
                    model.addAttribute("articleList", articleList);
                    return "index";
                }
            }
        }
        int pages = 1;
        List<MessageAndUser> messageAndUsers = messageService.queryMessageList("", "", pages);
        model.addAttribute("list", messageAndUsers);
        int count = messageService.queryMessageCount("", "");
        Page page = new Page();
        page.setTotalNumber(count);
        model.addAttribute("page", page);
        return "index";

    }

    @RequestMapping("/remove")
    public String removeUser(Model model, HttpServletRequest req) {
        req.getSession().removeAttribute("user");
        int pages = 1;
        List<MessageAndUser> messageAndUsers = messageService.queryMessageList("", "", pages);
        model.addAttribute("list", messageAndUsers);
        int count = messageService.queryMessageCount("", "");
        Page page = new Page();
        page.setCurrentPage(1);
        page.setTotalNumber(count);
        model.addAttribute("page", page);
        List<Article> articleList = articleService.queryArticleList("", pages);
        model.addAttribute("articleList", articleList);
        return "index";
    }

}
